package com.heima.news.controller;

import com.heima.news.domain.News;

import com.heima.news.domain.vo.NewsVO;
import com.heima.news.service.NewsService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/news")
public class NewsController extends BaseController {
    @Autowired
    private NewsService newsService;


    @GetMapping("/list")
    public TableDataInfo listNews(News news) {
        startPage();
        List<News> newsList = newsService.listNews(news);
        return getDataTable(newsList);
    }
    @ApiOperation("延迟发布新闻")
    @PostMapping("/{newsIds}")
    public AjaxResult delayPublishNews(@PathVariable List<Integer> newsIds) {
        Boolean result =newsService.delayPublishNews(newsIds);
        return toAjax(result);
    }


    //新增新闻管理
    @RequiresPermissions("news:add")
    @Log(title = "新闻管理", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增新闻")
    public AjaxResult add(@RequestBody News tbNews)
    {
        return toAjax(newsService.insertTbNews(tbNews));
    }

    /**
     * 修改新闻管理
     */
    @RequiresPermissions("system:news:edit")
    @Log(title = "新闻管理", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改新闻")
    public AjaxResult edit(@RequestBody News tbNews)
    {
        return toAjax(newsService.updateTbNews(tbNews));
    }

    /**
     * 删除新闻管理
     */
    @RequiresPermissions("system:news:remove")
    @Log(title = "新闻管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{newsIds}")
    @ApiOperation("删除新闻")
    public AjaxResult remove(@PathVariable Long[] newsIds)
    {
        return toAjax(newsService.deleteTbNewsByNewsIds(newsIds));
    }

    /**
     * 获取新闻管理详细信息
     */
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(newsService.getById(id));
    }

    /**
     * 删除旧新闻
     */
    @DeleteMapping("/deleteOldNews")
    public AjaxResult deleteOldNews() {
        newsService.deleteOldNews();
        return success();
    }

    /*
     * 通过noticeId修改新闻
     */
    @PutMapping("/notice")
    public AjaxResult updateByNoticeId(@RequestBody News tbNews) {
        newsService.updateByNoticeId(tbNews);
        return success();
    }

    @DeleteMapping("/notice/{noticeIds}")
    public AjaxResult removeByNoticeIds(@PathVariable("noticeIds") Long[] noticeIds) {
        return success(newsService.removeByNoticeIds(noticeIds));
    }
}
